// pages/auth/accountLogin/accountLogin.js
const app = getApp();
const api = require("../../../config/api")
const util = require("../../../utils/utils")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eysClose: true,
    passwordType: "password",
    username: '',
    password: ''
  },

  inputName(e) {
    this.setData({
      username: e.detail.value
    })
  },
  inputPasswd(e) {
    this.setData({
      password: e.detail.value
    })
  },
  accountLogin() {
    var that = this;
    if (this.data.password.length < 1 || this.data.username.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入用户名和密码',
        showCancel: false
      });
      return false;
    }
    wx.request({
      url: api.AuthLoginByAccount,
      data: {
        username: that.data.username,
        password: that.data.password
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.errno == 0) {
          app.globalData.hasLogin = true;
          wx.setStorageSync('userInfo', res.data.data.userInfo);
          wx.setStorage({
            key: "token",
            data: res.data.data.token,
            success: function () {
              // wx.switchTab({
              //   url: '/pages/user/user'
              // });
              wx.navigateBack({
                delta: 1,
              })
            }
          });
        } else {
          app.globalData.hasLogin = false;
          util.showErrorToast('账户登录失败');
        }
      }
    });
  },
  changeEye() {
    let flag = !this.data.eysClose;
    let types = "password";
    if (flag) {
      types = "password"
    }
    else {
      types = "text"
    }
    this.setData({
      eysClose: flag,
      passwordType: types
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})